package com.ulook.lookplanet.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TSysMsg;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

public class SysMsgService extends Service {

    private static volatile boolean RUNNING = true;
    private String sysMsgUrl = AppServer.ROOT_PATH + "/notif/querySysMsg";
    public final static String INTENT_ACTION = "com.ulook.lookplanet.service.SYS_MSG";
    private int currUserId;
    private Thread msgThread;
    LocalBroadcastManager localBroadcastManager;
    /*private SysMsgBinder sysMsgBinder = new SysMsgBinder();
    private Handler mHandler;*/

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("#####","SysMsgService");
        currUserId = intent.getIntExtra("currUserId", 0);
        if(currUserId == 0){
            return super.onStartCommand(intent, flags, startId);
        }
        RequestParams params = new RequestParams();
        params.put("userid", currUserId);

        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        msgThread = new Thread(new Runnable() {
            @Override
            public void run() {
                ResponseCallback callback = new ResponseCallback() {
                    @Override
                    public void onSuccess(Object res) {
                        //saveMsg(res);
                        try {
                            List<TSysMsg> sysMsgs = JacksonUtil.json2list((String)res, TSysMsg.class);
                            for (int i = 0; i < sysMsgs.size(); i++){
                                // 保存消息
                                sysMsgs.get(i).save();
                                // 发送广播
                                Log.d("#####","发送广播SysMsg");
                                Intent broadcastIntent = new Intent(INTENT_ACTION);
                                broadcastIntent.putExtra("msg", sysMsgs.get(i));
                                localBroadcastManager.sendBroadcast(broadcastIntent);
                                //sendBroadcast(broadcastIntent);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(OkHttpException failuer) {

                    }
                };

                while (RUNNING){
                    // 查询系统消息
                    AsynRequestUtil.post(sysMsgUrl, params, null, callback);

                    try {
                        Thread.sleep(15000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        msgThread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.d("#####","SysMsgServiceDDDDD");
        RUNNING = false;
        msgThread.interrupt();
        super.onDestroy();
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //return sysMsgBinder;
        return null;
    }

}